<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <%@include file="../common/head.jsp" %>
	<script type="text/javascript">
	$(document).ready(function(){
		$("#ckd").focus();
		$("#ckd").keypress(function(e) {  
			// 回车键事件
		   if(e.which == 13) {
				search();
		   }
		});

		$("#batchNo").keypress(function(e) {
			// 回车键事件
			
			if(e.which == 13) {
				find();
			}
		});
	});
	var sumQty = 0; //定义全局变量出库数量
	function search(){
		var data = {};
		data.ckd = $("#ckd").val();
		clearForm();
		$.ajax({
			type : 'POST',
			url : "<%=request.getContextPath()%>/salesConfirm/search",
			data : data,
			success : function(responseData) {
				if(responseData.result){
					$("#batchNo")[0].select();
					sumQty = responseData.QTYOUT;
					$("#sumQty").html(sumQty);
					$("#msg").html("<div style='color: #ff0000'>扫描成功</div>")
				} else {
					$("#ckd")[0].select();
					$("#msg").html("<div style='color: #ff0000'>" + responseData.msg + "</div>");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(errorThrown);
			},
			dataType : 'json'
		});
	}
	
	var batchCodes = [];
	var types = [];
	var qtys = [];
	function clearForm() {
		batchCodes = [];
		types = [];
		qtys = [];
		$("#tableView").html('<tr id="addTr" ></tr>');
		$("#productInfo").html('');
		$("#productId").val('');
		$("#batchNo").val('');
		$("#sumQty").html('');
	}
	
	function find(){
		var data = {};
		$("#msg").html("");
		data.code = $("#ckd").val();
		data.batchNo = $("#batchNo").val();
		data.existCodes = batchCodes.join(",");
		data.sumQty = sumQty;
/* 		data.dw = batchCodes;
		alert(dw); */
		if(!data.code || data.code==''){
			$("#msg").html("<div style='color: #ff0000'>请扫描出库单号！</div>");
			return;
		}
		$.ajax({
			type : 'POST',
			url : "<%=request.getContextPath()%>/salesConfirm/find",
			data : data,
			success : function(responseData) {
				$("#batchNo")[0].select();
				if(responseData.result){
					createTable(responseData.listMap, responseData.type, responseData);
					$("#msg").html("<div style='color: #ff0000'>扫描成功</div>");
				} else {
					$("#msg").html("<div style='color: #ff0000'>" + responseData.msg + "</div>");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(errorThrown);
			},
			dataType : 'json'
		});
	}
	
	function createTable(detailList, type, data){
		if(detailList){
			var trHtml = [];
			var dw = nvl(data.COLOR) + "/" + nvl(data.EFFICIENCY) + "/" + nvl(data.ELECTRICCURRENT);
			var product = "", productId = "";
			for(var i in detailList) {
				var qty = detailList[i].QTY;
				sumQty  = sumQty+qty;
				$("#sumQty").html(sumQty);
				productId = detailList[i].M_PRODUCT_ID;
				product = productId + "-" + detailList[i].NAME;
				var code = detailList[i].CODE;
				trHtml.push("<tr>");
				trHtml.push("<td align='center' >" + type + "</td>");
				trHtml.push("<td align='center' name='batchId'>" + code.substr(0, 11) + "<br>" + code.substr(11) + "</td>");
				trHtml.push("<td align='center'>" + qty + "</td>");
				trHtml.push("<td align='center'>" + dw + "</td>");
				trHtml.push("</tr>");
				
				
				batchCodes.push(code);
				types.push(type);
				qtys.push(qty);
			}
			$("#productInfo").html(product);
			$("#productId").val(productId);
			trHtml = trHtml.join("");
			$("#addTr").after(trHtml);
			
		}
/* 		$("td[name='batchId']").dblclick(function() {
					batchCodes.splice(code);
					types.splice(type);
					qtys.splice(qty);
					alert(qty);
					sumQty = sumQty - qty;
					$("#sumQty").html(sumQty);
					this.parentNode.remove();
		}); */
	}
	
	function submit(){
		$("#msg").html("");
		var data = {};
		var ckCode = $("#ckd").val();
		if(!ckCode || ckCode == ''){
			$("#msg").html("<div style='color: #ff0000'>请扫描出库单号！</div>");
			return;
		}
		if (batchCodes.length == 0) {
			$("#msg").html("<div style='color: #ff0000'>请至少扫描一个包装条码！</div>");
			return;
		}
		
		var data = {ckCode: ckCode, productId: $("#productId").val(), code: batchCodes, type: types, qty: qtys , sumQty: sumQty}; 
		$.ajax({
			type : 'POST',
			url : "<%=request.getContextPath()%>/salesConfirm/submit",
			data : data,
			success : function(responseData) {
				if(responseData.result){
					clearForm();
					$("#msg").html("<div style='color: #ff0000'>出库成功！</div>")
					$("#batchNo")[0].select();
				} else {
					$("#msg").html("<div style='color: #ff0000'>" + responseData.msg + "</div>");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert(errorThrown);
			},
			dataType : 'json'
		});
	}	
	</script>
</head>
<body>
	<input id="productId" type="hidden"/>
	<table style="width: 100%" cellpadding="3">
		<tr>
			<td colspan="2" style="background-color: #4E81ac;color: #ffffff;">
				<div style="float: left;margin: 2px;font-size: 15px;">销售出库</div>
				<div style="float: right;margin-right: 10px;" id="buttonDiv">
					<!-- <input onclick="cancel();" style="padding: 5px 10px 5px 10px;" type="button" value="取消"/> -->
				</div>
			</td>
		</tr>
		<tr>
			<td style="width: 70px;" align="right">出库单号：</td>
			<td style="width: 220px">
				<input type="text" id="ckd" maxlength="32" style="width: 95%;"/>
			</td>
		</tr>
		<tr>
			<td style="width: 70px;" align="right">条码：</td>
			<td style="width: 220px"><input id="batchNo" maxlength="32" style="width: 95%;"/></td>
		</tr>
		<tr>
			<td style="width: 70px;" align="right">物料：</td>
			<td style="width: 220px">
				<label id="productInfo"></label>
			</td>
		</tr>
		<tr>
			<td style="width: 70px;" align="right">扫描数量：</td>
			<td style="width: 220px" id="sumQty">
			</td>
		</tr>
		<tr>
			<td style="width: 70px;" align="right">提示：</td>
			<td style="width: 220px" id="msg"></td>
		</tr>
		<tr>
			<td id="material" colspan="2" style="width: ">
				<table style="width: 100%;" class="detail_table" id="matList" cellpadding="0" cellspacing="0">
				<thead>
					<tr>
						<th>类型</th>
						<th>包装条码</th>
						<th>数量</th>
						<th>档位</th>
					</tr>
				</thead>
				<tbody id="tableView">
					<tr id="addTr"></tr>
				</tbody>
				</table>
			</td>
		</tr>
		
		<tr>
			<td colspan="2" align="center">
				<input type="button" id="submitBtn" class="okButton" value="确定" onclick="javascript:submit();"/>
				&nbsp;&nbsp;
				<input type="button" id="closeBtn" class="cancelButton" value="关闭" onclick="javascript:history.back();"/>
			</td>
		</tr>
	</table>
</body>
</html>